package com.power.service.impl;

import com.mysql.jdbc.Connection;
import com.power.common.Contants;
import com.power.dao.UserDao;
import com.power.dao.impl.UserDaoImpl;
import com.power.ov.ResultData;
import com.power.pojo.Roles;
import com.power.pojo.Users;
import com.power.service.UsersService;
import com.power.utils.MD5;

import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/04 22:58
 * Description:
 * Version: V1.0
 */
public class UsersServiceImpl implements UsersService {
    UserDao userDao=new UserDaoImpl();
    @Override
    public ResultData findByAccount(String user_account, String old_pwd) throws SQLException {
        ResultData resultData=new ResultData();
        String new_pwd = null;
        try {
            new_pwd= MD5.getMD5(old_pwd).toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        List<Map<String,Object>> list=userDao.findByAccount(user_account,new_pwd);
        if (list.size()<=0){
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("密码不正确");
        }
        return resultData;
    }

    @Override
    public ResultData userUpdate(Users users) throws SQLException {
        ResultData resultData=new ResultData();
        int res=0;
        try {
            users.setUser_pwd(MD5.getMD5(users.getUser_pwd()).toString());
            res=userDao.userUpdate(users);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("修改失败");
        }
        return resultData;
    }

    @Override
    public ResultData queryRole() throws SQLException {
        ResultData resultData=new ResultData();
        List<Map<String,Object>> list=userDao.queryRole();
        if (list.size()>0){
            for (Map map:list) {
                map.get("update_time");
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                map.put("update_time",simpleDateFormat.format(map.get("update_time")));
            }
            resultData.setCode(Contants.TABLE_CODE);
            resultData.setMsg("成功");
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.EXCEPTION);
            resultData.setMsg("系统开小差了");
        }
        return resultData;
    }

    @Override
    public ResultData roleUpdate(Roles roles) throws SQLException {
        ResultData resultData=new ResultData();
        roles.setUpdate_time(new Date());
        int res=userDao.roleUpdate(roles);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("修改失败");
        }
        return resultData;
    }

    @Override
    public ResultData findByRoleName(String roleName) throws SQLException {
        ResultData resultData=new ResultData();
        int res=userDao.findByRoleName(roleName);
        if (res>=1){
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("角色名重复");
        }
        return resultData;
    }

    @Override
    public ResultData addRole(String roleName) throws SQLException {
        ResultData resultData=new ResultData();
        Date date=new Date();
        int res=userDao.addRole(roleName,date);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("添加失败");
        }
        return resultData;
    }

    @Override
    public ResultData queryByNameAndState(String roleName, String roleStatee) throws SQLException {
        ResultData resultData=new ResultData();
        List<Map<String,Object>> list=userDao.queryByNameAndState(roleName,roleStatee);
        if (list.size()>0){
            for (Map map:list) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (map.get("update_time")!=null){
                    map.put("update_time",simpleDateFormat.format(map.get("update_time")));
                }
            }
            resultData.setCode(Contants.TABLE_CODE);

            resultData.setMsg("查询成功");
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("不好意思没有这条数据哦！！！");
        }
        return resultData;
    }

    @Override
    public ResultData userQuery(String page, String limit) throws SQLException {
        ResultData resultData=new ResultData();
        List<Map<String,Object>> list=userDao.userQuery(page,limit);
        if (list.size()>0){
            int count=userDao.userCount();
            System.out.println("count+"+count);
            for (Map map:list) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
                if (map.get("lately_time")!=null){
                map.put("lately_time",simpleDateFormat.format(map.get("lately_time")));
                }if (map.get("in_time")!=null){
                map.put("in_time",simpleDateFormat1.format(map.get("in_time")));
                }
                if (map.get("out_time")!=null){
                map.put("out_time",simpleDateFormat1 .format(map.get("out_time")));
                }
            }
            resultData.setCode(Contants.TABLE_CODE);
            resultData.setMsg("查询成功");
            resultData.setCount(count);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("不好意思没有数据哦！！！");
        }
        return resultData;
    }

    @Override
    public ResultData updateUser(Users users) throws SQLException {
        ResultData resultData=new ResultData();
        int res=userDao.updateUser(users);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
        }else{
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("修改失败");
        }
        return resultData;
    }

    @Override
    public ResultData addUser(Users users) throws SQLException {
        ResultData resultData=new ResultData();
        try {
            String userPwd=MD5.getMD5(Contants.DEFAULT_PWD).toString();
            users.setUser_pwd(userPwd);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        int res = userDao.addUser(users);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("添加失败");
        }
        return resultData;
    }

    @Override
    public ResultData queryByUser(String user_name, String user_state, String page, String limit) throws SQLException {
        ResultData resultData=new ResultData();
        List<Map<String,Object>> list=userDao.queryByUser(user_name,user_state,page,limit);
        if (list.size()>0){
            int count=userDao.queryByUserCount(user_name,user_state);
            for (Map map:list) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
                if (map.get("lately_time")!=null){
                    map.put("lately_time",simpleDateFormat.format(map.get("lately_time")));
                }if (map.get("in_time")!=null){
                    map.put("in_time",simpleDateFormat1.format(map.get("in_time")));
                }
                if (map.get("out_time")!=null){
                    map.put("out_time",simpleDateFormat1 .format(map.get("out_time")));
                }
            }
            resultData.setCode(Contants.TABLE_CODE);
            resultData.setMsg("查询成功");
            resultData.setCount(count);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("不好意思没有这条数据哦！！！");
        }
        return resultData;
    }

    @Override
    public ResultData deleteUser(String userAccount) throws SQLException {
        ResultData resultData=new ResultData();
        int res=userDao.deleteUser(userAccount);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("删除成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("删除失败");
        }
        return resultData;
    }

    @Override
    public ResultData judgeUserAcc(String userAccount) throws SQLException {
        ResultData resultData=new ResultData();
        List<Map<String,Object>> list=userDao.judgeUserAcc(userAccount);
        if (list.size()>0){
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("用户账号重复");
        }
        return resultData;
    }

    @Override
    public ResultData queryAuthorityAll(String roleId) throws SQLException {
        ResultData resultData=new ResultData();
        List listAll=new ArrayList();
        List<Map<String,Object>> list=userDao.queryAuthorityAll(roleId);
        List<Map<String,Object>> list2=userDao.queryAuthorityAll2();
        if (list.size()>0 && list2.size()>0){
            listAll.add(list);
            listAll.add(list2);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("成功");
            resultData.setData(listAll);
        }
        return resultData;
    }

    @Override
    public ResultData updateAuthority(String roleId, List<Map> list) throws SQLException {
        ResultData resultData=new ResultData();
        int res=userDao.updateAuthority(roleId,list);
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("修改失败");
        }
        return resultData;
    }
}
